package com.bbx.taxi.client.Task;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.official.passanger.Officail_VclassBuild;
import com.bbx.api.sdk.model.official.passanger.Official_OperateBuild;
import com.bbx.api.sdk.model.official.passanger.Return.Officail_Vehicle;
import com.bbx.api.sdk.model.official.passanger.Return.Officail_Vehicle.VehicleList;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.OfficialOrderList;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.official.passanger.Officail_VclassNet;
import com.bbx.api.sdk.net.official.passanger.Officail_VehicleNet;
import com.bbx.api.sdk.net.official.passanger.Official_OperateNet;
import com.bbx.taxi.client.Activity.Menu.MyOrderActivity;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.DB.MyOrder.MyOrderDB;
import com.bbx.taxi.client.DB.MyOrder.MyOrderingDB;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.official.R;

/** 
 * @Path com.bbx.taxi.client.Task.MyOperateTask.java
 * @Author 严旭文
 * @Date 2016-1-28 下午3:08:23
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 */
public class MyOperateTask{
	public static int STATUS_SUCCESS=0;
	public static int STATUS_FAIL=-1;
	private Activity context;
	public static MyOperateTask instance;
	public OperateTask mOperateTask;
	public interface OnOperateStatusListener {
		/**
		 *status 0为成功
		 */
		public void onOperateStatus(int status);
	}

	static OnOperateStatusListener mOnOperateStatusListener = null;

	public static void setOnOperateStatusListener(OnOperateStatusListener l) {
		mOnOperateStatusListener = l;
	}
	public static MyOperateTask getInstance(Activity context,
			MyApplication mApplication,String order_id,int opcode,String reason) {
		instance = new MyOperateTask(context,order_id,opcode,reason);
		return instance;
	}
	public MyOperateTask(Activity context,String order_id,int opcode,String reason) {
		this.context = context;
		getOperate(order_id,opcode,reason);
	}
	/**
	 * 获取车型列表
	 */
	public void getOperate(String order_id,int opcode,String reason) {
		Official_OperateBuild mOfficial_OperateBuild = new Official_OperateBuild(context);
		mOfficial_OperateBuild.opuid=MyApplication.getInstance().getUid();
		mOfficial_OperateBuild.order_id=order_id;
		mOfficial_OperateBuild.opcode=opcode;
		mOfficial_OperateBuild.reason=reason;
		mOperateTask = new OperateTask(context, mOfficial_OperateBuild);
		mOperateTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}
	private class OperateTask extends BaseAsyncTask {

		private Official_OperateBuild mOfficial_OperateBuild;

		public OperateTask(Activity context, Official_OperateBuild mOfficial_OperateBuild) {
			super(context);
			this.mOfficial_OperateBuild = mOfficial_OperateBuild;
		}

		@Override
		public void onSuccess(Object object) {
			int opcode=mOfficial_OperateBuild.opcode;
			switch(opcode){
			case Value.PASS:
				ToastUtil.showToast(context, "通过成功");
				break;
			case Value.NOPASS:
				new AsyncTask<Void, Void, Void>(){
					MyOrderDB db_order = new MyOrderDB(context);
					MyOrderingDB db_ordering = new MyOrderingDB(context);
					@Override
					protected Void doInBackground(Void... arg0) {
						return null;
					}}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
				
				
				ToastUtil.showToast(context, "拒绝成功");
				break;
			
			}
	
			if (mOnOperateStatusListener != null) {
				//存储数据库，由于审核列表跟订单列表数据不一样，，暂时不能用本地存储，先改成网络读取全部的订单在存储
//				db_myordering.onWriteDB_ing(context, mOfficialOrderList);
				MyOrderTask.getInstance(context, MyOrderActivity.num_ing,Value.TYPE_ING,Value.OWNALL,MyOrderTask.TYPE_REFRESH);
				mOnOperateStatusListener.onOperateStatus(STATUS_SUCCESS);
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			if(status==-2){
				ToastUtil.showToast(context, "该订单已由其他审核员处理了，请刷新列表");
			}else{
				ToastUtil.showToast(context, R.string.no_network);
				if (mOnOperateStatusListener != null) {
					mOnOperateStatusListener.onOperateStatus(STATUS_FAIL);
				}
			}
		
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mOfficial_OperateBuild).getJson();
			return new Official_OperateNet(context, json);
		}
	}

}
